var database = require('../../data/data.js');
Page({
  data: {
    pageNum: 1
  },

  onLoad: function (options) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    var date = new Date();
    var month = "";
    if (date.getMonth() < 9) {
      month = "0" + (parseInt(date.getMonth()) + 1)
    } else {
      month = parseInt(date.getMonth()) + 1
    }
    var date1 = date.getFullYear() + "-" + month+"-01";
    this.setData({
      year: date.getFullYear(),
      month: month,
      date1:date1
    })
    var pageNum = this.data.pageNum;
    wx.request({
      url: database.url + '/api/V2/integral/integral_list',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        pageNo: pageNum,
        pageSize: 10,
        unionid: database.unionid,
        yearMonth: that.data.year + that.data.month
      },
      success(res) {
        console.log(res.data)
        ++pageNum;
        var integralList = res.data.data.integralList;
        that.setData({
          integralList: integralList,
          pageNum: pageNum
        })
        if (integralList.length < 10) {
          that.setData({
            noData: true
          })
        }
        wx.hideLoading();
      }
    })
  },
  search: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/search/search?date=' + that.data.year + that.data.month,
    })
  },
  dateChange: function (e) {
    wx.showLoading({
      title: '资源加载中',
    })
    var that = this;
    var date = e.detail.value;
    var year = date.substring(0, 4);
    var month = date.substring(5);
    this.setData({
      year: year,
      month: month,
      pageNum: 1
    })
    var pageNum = this.data.pageNum;
    wx.request({
      url: database.url + '/api/V2/integral/integral_list',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        regionName: "other",
        pageNo: pageNum,
        pageSize: 10,
        unionid: database.unionid,
        yearMonth: that.data.year + that.data.month
      },
      success(res) {
        if (res.data.data.integralList != null) {
          ++pageNum;
          var integralList = res.data.data.integralList;
          that.setData({
            integralList: integralList,
            pageNum: pageNum
          })
          if (integralList.length < 10) {
            that.setData({
              noData: true
            })
          }else{
            that.setData({
              noData: false
            })
          }
        } else {
          that.setData({
            noData: true,
            integralList: ""
          })
        }
        wx.hideLoading();
      }
    })
  },
  toDetail: function (e) {
    var date = this.data.year + this.data.month
    wx.navigateTo({
      url: '/pages/team_detail/team_detail?id=' + e.currentTarget.dataset.id + '&type=history&date=' + date,
    })
  },
  onReachBottom: function () {
    var that = this;
    var integralList1 = [];
    var pageNum = that.data.pageNum;
    if (!this.data.noData) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      wx.request({
        url: database.url + '/api/V2/integral/integral_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          regionName: "other",
          pageNo: pageNum,
          pageSize: 10,
          unionid: database.unionid,
          yearMonth: that.data.year + that.data.month
        },
        success(res) {
          ++pageNum;
          var integralList2 = res.data.data.integralList;
          for (var i = 0; i < that.data.integralList.length + integralList2.length; i++) {
            if (i < that.data.integralList.length) {
              integralList1[i] = that.data.integralList[i];
            } else {
              integralList1[i] = integralList2[i - that.data.integralList.length];
            }
          }
          that.setData({
            integralList: integralList1,
            pageNum: pageNum
          })
          if (integralList2.length < 10) {
            that.setData({
              noData: true
            })
          }
          wx.hideLoading();
        }
      })
    }
  },
})